package com.cjh.common.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cjh.common.dto.CompleteOrderTimingTaskDTO;
import com.cjh.common.dto.StoreOrderDto;
import com.cjh.common.entity.StoreOrder;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Entity com.cjh.common..StoreOrder
 */
public interface StoreOrderMapper extends BaseMapper<StoreOrder> {
    //SYS
    List<StoreOrderDto> getPagingStore(Integer start, Integer size, Integer userId, Integer status, String number, Date startTime, Date endTime, Integer storeId);

    Integer getPagingStoreTotal(Integer userId, Integer status, String number, Date startTime, Date endTime, Integer storeId);

    StoreOrderDto getStoreByOrderId(Integer orderId);

    //WX
    List<StoreOrderDto> getAllOrderList(Integer current, Integer size, Integer isNowDay,Integer userId);

    //task
    List<CompleteOrderTimingTaskDTO> selectCompleteOrderTimingTask();

    List<CompleteOrderTimingTaskDTO> selectRefundOrderTimingTask();
    //所有的数据
    @Select("select count(order_id) as AllCount,sum(all_price) as AllPrice from store_order")
    Map<String, Object> selectAllData();
    //今天的数据
    @Select("select count(order_id) as TodayCount,sum(all_price) as TodayPrice from store_order where to_days(create_time) = to_days(now())")
    Map<String, Object> selectTodayData();
    //昨天的数据
    @Select("select count(order_id) as YesterdayCount,sum(all_price) as YesterdayPrice from store_order where TO_DAYS( NOW( ) ) - TO_DAYS( create_time) <= 1")
    Map<String, Object> selectYesterdayData();
    //近七天的数据
    @Select("select count(order_id) as RecentSevenDaysCount,sum(all_price) as RecentSevenDaysPrice from store_order where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(create_time)")
    Map<String, Object> selectRecentSevenDaysData();
    //本月的数据
    @Select("select count(order_id) as InThisMonthCount,sum(all_price) as InThisMonthPrice from store_order where DATE_FORMAT( create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )")
    Map<String, Object> selectInThisMonthData();

    @Select("SELECT  DATE_FORMAT( date, '%m-%d' ) as RT, IFNULL(tab.num, 0) as RN,IFNULL(tab.price, 0) as RM FROM (\n" +
            "SELECT\n" +
            "\t@cdate := date_add( @cdate, INTERVAL - 1 DAY ) as date\n" +
            "FROM\n" +
            "\t( SELECT @cdate := date_add(CURDATE(), INTERVAL + 1 DAY ) \n" +
            "\tFROM store_order\n" +
            "\t)d1\n" +
            "\tWHERE YEAR(@cdate)=YEAR(CURDATE()) \n" +
            "\tAND MONTH(@cdate)=MONTH(CURDATE()) \n" +
            "\tAND DAY(@cdate) > 1\n" +
            "\tORDER BY date\n" +
            ")date_c LEFT JOIN (\n" +
            "SELECT COUNT(*) as num,SUM(all_price) as price, DATE(create_time) as t\n" +
            "FROM store_order WHERE YEAR(create_time)=YEAR(CURDATE()) AND MONTH(create_time)=MONTH(CURDATE()) and store_id = #{storeId} GROUP BY DATE(create_time) \n" +
            ")tab ON DATE(t)=date\n" +
            "ORDER BY date asc;")
    List<Map<String, Object>> selectEchartData(Integer storeId);
}




